package com.ugoodtech.mdcc.core.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Created by Lance Li on 2020/1/15.
 *
 * @author Lance Li
 */
public class ConverterUtils {

    public static final String DEFAULT_SPLIT_CHAR = ",";

    public static List<Long> stringListToLongList(List<String> strings) {
        if (strings == null)  {
            return null;
        }
        List<Long> longList = new ArrayList<>(strings.size());
        for (String string : strings) {
            longList.add(Long.valueOf(string));
        }
        return longList;
    }

    public static List<Long> stringToLongList(String strings, String splitChar) {
        if (strings == null) {
            return null;
        }
        if (strings.length() == 0) {
            return new ArrayList<>(0);
        }
        if (splitChar == null) {
            splitChar = DEFAULT_SPLIT_CHAR;
        }
        String[] stringArray = strings.split(splitChar);
        List<Long> longList = new ArrayList<>(stringArray.length);
        for (String string : stringArray) {
            longList.add(Long.valueOf(string));
        }
        return longList;
    }

    public static List<Integer> stringToIntegerList(String strings, String splitChar) {
        if (strings == null) {
            return null;
        }
        if (strings.length() == 0) {
            return new ArrayList<>(0);
        }
        if (splitChar == null) {
            splitChar = DEFAULT_SPLIT_CHAR;
        }
        String[] stringArray = strings.split(splitChar);
        List<Integer> longList = new ArrayList<>(stringArray.length);
        for (String string : stringArray) {
            if (StringUtils.isNotEmpty(string)) {
                longList.add(Integer.valueOf(string));
            }
        }
        return longList;
    }

    public static List<Long> stringSplitByCommaToLongList(String strings) {
        return stringToLongList(strings, ",");
    }

    public static List<String> stringArrayToStringList(String strings, String splitChar) {
        if (strings == null) {
            return null;
        }
        if (strings.length() == 0) {
            return new ArrayList<>(0);
        }
        if (splitChar == null) {
            splitChar = DEFAULT_SPLIT_CHAR;
        }
        String[] stringArray = strings.split(splitChar);
        List<String> stringList = new ArrayList<>(stringArray.length);
        stringList.addAll(Arrays.asList(stringArray));
        return stringList;
    }

    public static List<String> stringSplitByCommaToStringList(String strings) {
        return stringArrayToStringList(strings, ",");
    }

    public static String stringListToStringSplitByComma(List<String> strings) {
        if (strings == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : strings) {
            stringBuilder.append(string).append(",");
        }
        if (stringBuilder.length() > 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }

    public static String listToString(List<?> list, String splitChar) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Object o : list) {
            stringBuilder.append(o).append(splitChar);
        }
        if (stringBuilder.length() > 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }

}
